Repository navigation
fix(db): collapse segment memberships per merge group in normalize_contact_emails migration - #593
Open
pcbeingused333 wants to merge 1 commit into
Conversation
…ntact_emails migration The dedupe DELETE only removed a duplicate's membership when the survivor already held one, so two duplicates sharing a segment the survivor was not in both got rewritten to the same (contactId, segmentId) and hit the primary key, aborting the upgrade. It also kept an exited survivor row over active duplicate rows, silently dropping the merged contact from the segment. Keep one row per (survivor, segment) across the whole group, preferring an active membership.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #436
Description
Root cause. In
20260615120000_normalize_contact_emails, the segment-membership dedupeDELETEonly removes a duplicate's row when the survivor already holds one for that segment. If two duplicates share a segment the survivor is not in, neither row is deleted, the followingUPDATErewrites both to(survivor, segment)and violatessegment_memberships_pkey, soprisma migrate deployfails and the container does not start. The sameDELETEalso keeps the survivor's row even when it is exited (exitedAtset) and deletes active duplicate rows, silently dropping the merged contact from the segment.Fix. Replace the
DELETEwith one that keeps a single row per(survivor, segment)across the whole merge group (survivor included), preferring an active membership (exitedAt IS NULL), then the oldestenteredAt. The existing reassignmentUPDATEis then collision-free. With an empty merge map the join matches nothing, so databases without duplicates are untouched. The SQL is the one @hanamizuki proposed in the issue.Type of Change
feat:New feature (MINOR version bump)fix:Bug fix (PATCH version bump)feat!:Breaking change - new feature (MAJOR version bump)fix!:Breaking change - bug fix (MAJOR version bump)docs:Documentation update (no version bump)chore:Maintenance/dependencies (no version bump)refactor:Code refactoring (no version bump)test:Adding tests (no version bump)perf:Performance improvement (PATCH version bump)Testing
The migration file was run against PostgreSQL with the reproduction from #436 (3 contacts in one merge group;
segA: survivor exited, both duplicates active;segB: survivor absent, both duplicates present). Before the change it aborts withduplicate key value violates unique constraint "segment_memberships_pkey"; after it commits and the survivor holds exactly one active membership in bothsegAandsegB. The repo's own test suite was not run (SQL-only change).Checklist
This PR was prepared with AI assistance (Claude Code) and submitted by a human contributor who is responsible for it.